﻿using System.Collections.Generic;
using System.Windows.Controls.Primitives;
using personalplaner.business.enums;
using System.Windows;
using personalplaner.common.mvvm;
using System.Windows.Controls;

namespace personalplaner.common.controls
{
    public class ControlContainer : Selector
    {
		static ControlContainer()
        {
			DefaultStyleKeyProperty.OverrideMetadata(typeof(ControlContainer), new FrameworkPropertyMetadata(typeof(ControlContainer)));
        }

		//public ControlContainer()
		//{
		//    Unloaded += ControlContainer_Unloaded;
		//}

		//void ControlContainer_Unloaded(object sender, RoutedEventArgs e)
		//{
		//    var vm = DataContext as BaseViewModel;
		//    if (vm == null)
		//        return;

		//    vm.Unloaded();
		//}

		public void AddItem(enDisplayItemType itemType, object item, bool overwrite = false)
        {
			if (DisplayabelItems.ContainsKey(itemType) && overwrite)
			{
				var itm = DisplayabelItems[itemType];
				DisplayabelItems.Remove(itemType);
				Items.Remove(itm);
			}

			if (DisplayabelItems.ContainsKey(itemType))
				return;

			Items.Add(item);
            DisplayabelItems.Add(itemType, item);

			//SelectControl(itemType);
        }

        public bool SelectControl(enDisplayItemType itemType)
        {
            if (!ContainesItem(itemType))
                return false;

			//SetIsSelected(DisplayabelItems[itemType] as DependencyObject, true);
			SelectedItem = DisplayabelItems[itemType];
            return true;
        }

        public bool ContainesItem(enDisplayItemType itemType)
        {
            return DisplayabelItems.ContainsKey(itemType);
        }

        Dictionary<enDisplayItemType, object> _displayabelItems;
        Dictionary<enDisplayItemType, object> DisplayabelItems
        {
            get
            {
                if (_displayabelItems == null)
                    _displayabelItems = new Dictionary<enDisplayItemType, object>();
                return _displayabelItems;
            }
        }

        public bool IsSelected
        {
            get
            {
                return (bool)GetValue(IsSelectedProperty);
            }
            set
            {
                SetValue(IsSelectedProperty, value);
            }
        }

        //public static readonly DependencyProperty TitleProperty =
        //    DependencyProperty.Register("IsSelected", typeof(string), typeof(CollapsibleGroupBox), new PropertyMetadata(false));

		public void Remove(UserControl uc)
		{
			foreach (var key in DisplayabelItems.Keys)
			{
				if (DisplayabelItems[key] == uc)
				{
					DisplayabelItems.Remove(key);
					break;
				}
			}
		}

		public void Remove(enDisplayItemType key)
		{
			if (DisplayabelItems.ContainsKey(key))
				DisplayabelItems.Remove(key);
		}

		public enDisplayItemType GetKey(UserControl uc)
		{
			foreach (var key in DisplayabelItems.Keys)
			{
				if (DisplayabelItems[key] == uc)
					return key;
			}

			return enDisplayItemType.None;
		}
    }
}
